package com.yunhe.authority.controller.marketing;

import com.yunhe.authority.domain.authority.UserAdditionalInfo;
import com.yunhe.authority.domain.wechat.Code2Session;
import com.yunhe.authority.service.wechat.WechatService;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.UsernamePasswordException;
import com.yunhe.common.model.ResultObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @desc 营销小程序登录Controller
 * @author zhengbo
 * @date 2021/2/2 16:14
 */
@Api(value = "marketingLogin", description = "营销小程序登录", tags = {"marketingLogin"})
@RestController
@RequestMapping("/wechatMarketingLogin")
public class MarketingLoginController {

    @Autowired
    private WechatService wechatService;

    /**
     * 根据用户名和密码获得用户（执行登录）
     * @param name 键
     * @param password 值
     */
    @ApiOperation("根据用户名和密码获得用户（执行登录）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "string", paramType = "query")
    })
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ResultObject> login(@RequestParam("name") String name,
                                              @RequestParam("password") String password,
                                              @RequestParam("phone") String phone)
            throws ArgumentErrorException, UsernamePasswordException {

        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    /**
     * 微信换取session_key接口
     * @param js_code 登录凭证
     */
    @ApiOperation("微信换取session_key接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "js_code", value = "登录凭证", dataType = "string", paramType = "path"),
    })
    @RequestMapping(value = "/code2Session/{js_code}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> code2Session(@PathVariable("js_code") String js_code) throws IOException, ArgumentErrorException {

        //TODO 需要与微信进行通信
        Code2Session code2Session = wechatService.getAuthInfo(js_code);
        return new ResponseEntity<>(new ResultObject<>(code2Session), HttpStatus.OK);
    }

    /**
     * 根据用手机号获得用户（执行登录）
     * @param phone 键
     */
    @ApiOperation("根据用户名和密码获得用户（执行登录）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/loginByPhone/{phone}",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> loginByPhone(@PathVariable(value = "phone") String phone)
            throws ArgumentErrorException, UsernamePasswordException {

        //TODO 需要与微信进行通信

        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    /**
     * 实名认证
     * @param userAdditionalInfo 身份信息扩展
     */
    @ApiOperation("根据用户名和密码获得用户（执行登录）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/verified",method = RequestMethod.POST)
    public ResponseEntity<ResultObject> verified(@RequestBody UserAdditionalInfo userAdditionalInfo)
            throws ArgumentErrorException, UsernamePasswordException {


        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

}
